package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * @ClassName ResourceProvider
 * @Description 资源加载器
 * @Author MingChang.Wei
 * @Date 2024/9/19
 **/
@Component
public class ResourceProvider {
    @Autowired
    private ResourceLoader resourceLoader;

    public String getResourcePath(String resourceName) {
        Resource resource = resourceLoader.getResource("classpath:templates/" + resourceName);
        try {
            return Paths.get(resource.getFile().toURI()).toString();
        } catch (IOException e) {
            throw new RuntimeException("无法获取资源路径", e);
        }
    }

    public String getResourceDir(String resourceName) {
        Resource resource = resourceLoader.getResource("classpath:templates/" + resourceName);
        try {
            return resource.getFile().getParent();
        } catch (IOException e) {
            throw new RuntimeException("无法获取资源路径", e);
        }
    }
}
